package by.fedorenko.sample.services.impl;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.fedorenko.sample.dataaccess.AppartmentDao;
import by.fedorenko.sample.datamodel.Appartment;
import by.fedorenko.sample.services.AppartmentService;


@Service
public class AppartmentServiceImpl implements AppartmentService {

	private static final Logger LOGGER = LoggerFactory.getLogger(AppartmentServiceImpl.class);

	@Inject
	private AppartmentDao appartmentDao;

	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of ProductService is created. Class is: {}", getClass().getName());
	}

	@Override
	public Appartment get(Long id) {
		Appartment entity = appartmentDao.getById(id);
		return entity;
	}

	

	@Override
	public void delete(Appartment appartment) {
		LOGGER.debug("Remove: {}", appartment);
		appartmentDao.delete(appartment.getId());

	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all products");
		appartmentDao.deleteAll();
	}

	@Override
	public void saveOrUpdate(Appartment appartment) 
	{
		if (appartment.getId() == null) {
			LOGGER.debug("Save new: {}", appartment);
			appartmentDao.insert(appartment);
		} else {
			LOGGER.debug("Update: {}", appartment);
			appartmentDao.update(appartment);
		
	}

	

}}